// Copyright (c) 2007 Robin Boerdijk. All rights reserved.
//
// This software is licensed under the terms of the Microsoft Reciprocal License as described 
// in the file LICENSE.txt, which you should have received as part of this distribution. The 
// license terms are also available at http://www.opensource.org/licenses/ms-rl.html.

#include "wtkpc.h"

#include "wtk/NumberEditControl.h"

#include <cctype>

namespace wtk
{
	NumberEditControl::NumberEditControl(DialogWindow *dialogWindow, int maxDigits)
		: EditControl(dialogWindow)
		, m_maxDigits(maxDigits)
	{
		this->style |= ES_RIGHT;
	}

	SIZE NumberEditControl::GetMinimumSize()
	{
		SIZE minimumSize = Window::GetTextBoxSize(std::wstring(m_maxDigits, L'5'));
		minimumSize.cx += 4;
		minimumSize.cy += 4;
		if (this->style | WS_BORDER) {
			minimumSize.cy += 2;
		}
		return minimumSize;
	}

	bool NumberEditControl::AcceptChar(wchar_t ch)
	{
		if (iswdigit(ch) || iswcntrl(ch)) {
			return true;
		} else if (ch == L'.') {
			wchar_t buffer[1024];
			Window::GetText(buffer, 1024);
			return (wcschr(buffer, L'.') == NULL);
		} else {
			return false;
		}
	}
}
